package com.jjkj.environmental.environmental.presenter

import com.jjkj.environmental.environmental.bean.model.CarInfo
import com.jjkj.environmental.environmental.bean.model.CheckInfo
import com.jjkj.environmental.environmental.model.NetWorkModel
import com.jjkj.environmental.environmental.presenter.base.BasePresenter
import com.jjkj.environmental.environmental.utils.MyLog
import com.jjkj.environmental.environmental.view.CheckView
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import javax.inject.Inject

class CheckPresenter : BasePresenter<CheckView>() {
    @Inject
    lateinit var netWorkModel: NetWorkModel

    override fun initInject() {
        getComponent().inject(this)
    }


    fun makePicture(carInfo: CarInfo) {
        netWorkModel.makePicture(carInfo = carInfo, observer = object : Observer<String> {
            override fun onComplete() {
                MyLog.i("LoginPresenter", "onComplete")
            }

            override fun onSubscribe(d: Disposable) {
                bindRxLife(d)
            }

            override fun onNext(t: String) {
                getMvpView().onDataLoad(t)
            }

            override fun onError(e: Throwable) {
                getMvpView().showException(e)
                MyLog.i("LoginPresenter", "onError")
            }
        })
    }

    fun closeVideo(carInfo: CarInfo) {
        netWorkModel.closeVideo(carInfo = carInfo, observer = object : Observer<String> {
            override fun onComplete() {
                MyLog.i("LoginPresenter", "onComplete")
            }

            override fun onSubscribe(d: Disposable) {
                bindRxLife(d)
            }

            override fun onNext(t: String) {
                getMvpView().onDataLoad(t)
            }

            override fun onError(e: Throwable) {
                getMvpView().showException(e)
                MyLog.i("LoginPresenter", "onError")
            }
        })
    }

    fun uploadPicture(carInfo: CarInfo, path: String, zpzl: String, position: Int) {
        netWorkModel.uploadPicture(carInfo = carInfo, path = path, zpzl = zpzl, observer = object : Observer<String> {
            override fun onComplete() {
                MyLog.i("LoginPresenter", "onComplete")
            }

            override fun onSubscribe(d: Disposable) {
                bindRxLife(d)
            }

            override fun onNext(t: String) {
                getMvpView().onPictureUpload(position)
            }

            override fun onError(e: Throwable) {
                getMvpView().onPictureUploadFailed(position, e.message ?: "照片上传失败")
            }
        })
    }

    fun uploadCheckInfo(carInfo: CarInfo, checkInfo: CheckInfo) {
        netWorkModel.uploadCheckInfo(carInfo = carInfo, checkInfo = checkInfo, observer = object : Observer<String> {
            override fun onComplete() {
                MyLog.i("LoginPresenter", "onComplete")
            }

            override fun onSubscribe(d: Disposable) {
                bindRxLife(d)
            }

            override fun onNext(t: String) {
                getMvpView().onDataLoad(t)
            }

            override fun onError(e: Throwable) {
                getMvpView().showException(e)
                MyLog.i("LoginPresenter", "onError")
            }
        })
    }
}